/*
 *处理player的连接请求
 */
package labox.innovation.loginserver.socket;

import java.nio.ByteBuffer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.loginserver.socket.LoginClient.LoginClientState;
import labox.innovation.loginserver.socket.clientpackets.AuthGameGuard;
import labox.innovation.loginserver.socket.clientpackets.ClientRegisterPacket;
import labox.innovation.loginserver.socket.clientpackets.LoginClientPacket;
import labox.innovation.loginserver.socket.clientpackets.RequestAuthLogin;
import labox.innovation.loginserver.socket.clientpackets.RequestBindAccount;
import labox.innovation.loginserver.socket.clientpackets.RequestChangePassWord;

import labox.innovation.netcore.IPacketHandler;
import labox.innovation.netcore.ReceivablePacket;

/**
 * Handler for packets received by Login Server
 * 
 * @author KenM
 */
public final class LoginPacketHandler implements IPacketHandler<LoginClient> {

	protected static final Logger _log = LoggerFactory.getLogger(LoginPacketHandler.class.getName());

	public ReceivablePacket<LoginClient> handlePacket(ByteBuffer buf, LoginClient client) {
		int opcode = buf.get() & 0xFF;

		ReceivablePacket<LoginClient> packet = null;
		LoginClientState state = client.getState();

		switch (state) {
		case CONNECTED: //-- 客户端请求授权协议
			if (opcode == LoginClientPacket.PTL_AUTHGAMEGUARD) {
				packet = new AuthGameGuard();
			} else {
				debugOpcode(opcode, state);
			}
			break;
		case AUTHED_GG: //-- 客户端请求提交账户密码请求登陆 
			switch (opcode) {
			case LoginClientPacket.PTL_CLIENTREGISTERPACKET:
				packet = new ClientRegisterPacket();
				break;
			case LoginClientPacket.PTL_REQUESTAUTHLOGIN:
				packet = new RequestAuthLogin();
				break;
			case LoginClientPacket.PTL_REQUESTCHANGEPASSWORD:
				packet = new RequestChangePassWord();
				break;
			case LoginClientPacket.PTL_REQUESTBINDPHONENO:
				packet = new RequestBindAccount();
				break;
			default:
				debugOpcode(opcode, state);
				break;
			}
			break;
		case AUTHED_LOGIN: //-- 请求服务器列表
			break;
		}

		/*   
		 
		   	switch (state) {
		   	
			case CONNECTED:  //-- 客户端请求授权协议
				if (opcode == 0x00) {
					packet = new SessionKeyAndVersionCheck();
				} else {
					debugOpcode(opcode, state);
				}
				break;
			case AUTHED_GG:  //-- 客户端请求提交账户密码请求登陆 
				if (opcode == 0x01) {
					packet = new RequestAuthLogin(); //-- 获取鉴权包
				} else {
					debugOpcode(opcode, state);
				}
				break;
			case AUTHED_LOGIN: //-- 请求服务器列表
				if (opcode == 0x05) {
					packet = new RequestServerList(); //-- 获取服务器列表包
				} else if (opcode == 0x02) {
					packet = new RequestServerLogin(); //-- 获取登陆包
				} else {
					debugOpcode(opcode, state);
				}
				break;
			}
		   
		   
		   */

		return packet;
	}

	private void debugOpcode(int opcode, LoginClientState state) {
		_log.info("Unknown Opcode:" + opcode + " for state: " + state.name(), Config.SERVER_ID);
	}
}
